package com.frank.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frank.reader.mapper.BookMapper;
import com.frank.reader.mapper.EvaluationMapper;
import com.frank.reader.mapper.MemberReadStateMapper;
import com.frank.reader.pojo.Book;
import com.frank.reader.pojo.Evaluation;
import com.frank.reader.pojo.MemberReadState;
import com.frank.reader.service.BookService;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("bookService")
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;

    @Resource
    private MemberReadStateMapper memberReadStateMapper;

    @Resource
    private EvaluationMapper evaluationMapper;

    /**
     * @param categoryId 分类ID
     * @param order      排序规则
     * @param page       页号
     * @param rows       每页记录数
     * @return IPage对象
     */
    @Override
    public IPage<Book> paging(Long categoryId, String order, Integer page, Integer rows) {
        Page<Book> bookPage = new Page<>(page, rows);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (categoryId != null && categoryId != -1) {
            queryWrapper.eq("category_id", categoryId);
        }
        if ("quantity".equals(order)) {
            queryWrapper.orderByDesc("evaluation_quantity");
        } else if ("score".equals(order)) {
            queryWrapper.orderByDesc("evaluation_score");
        }

        return bookMapper.selectPage(bookPage, queryWrapper);
    }

    @Override
    public Book selectById(Long categoryId) {
        Book book = bookMapper.selectById(categoryId);
        return book;
    }

    @Transactional // 开启声明式事务
    @Override
    public void updateEvaluation() {
        // 严格按层调用
        bookMapper.updateEvaluation();
    }

    @Transactional
    @Override
    public Book createBook(Book book) {
        bookMapper.insert(book);
        return book; // 多了图书编号
    }

    /**
     * 更新图书
     *
     * @param book 新图书数据
     * @return 更新后的图书
     */
    @Override
    @Transactional
    public Book updateBook(Book book) {
        bookMapper.updateById(book);
        return book;
    }

    /**
     * 删除图书及相关数据
     *
     * @param bookId 图书编号
     */
    @Transactional
    @Override
    public void deleteBook(Long bookId) {
        bookMapper.deleteById(bookId);
        QueryWrapper<MemberReadState> mrsQueryWrapper = new QueryWrapper<>();
        mrsQueryWrapper.eq("book_id", bookId);
        memberReadStateMapper.delete(mrsQueryWrapper);
        QueryWrapper<Evaluation> evaluationQueryWrapper = new QueryWrapper<>();
        evaluationQueryWrapper.eq("book_id", bookId);
        evaluationMapper.delete(evaluationQueryWrapper);
    }
}
